package com.zyx.mpdemo.model.vo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.util.List;

/**
 * <p>课程耗时
 *
 * @author Yaxi.Zhang
 * @since 2022/11/21 14:20
 */
@Data
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
public class CourseCost {
    /** 小时数 */
    private Integer hours;

    /** 分钟数 */
    private Integer minutes;

    /** 秒数 */
    private Integer seconds;

    /** 获取课程秒数 */
    public Integer getCourseSeconds() {
        return this.hours * 60 * 60 + this.minutes * 60 + this.seconds;
    }

    /** 通过总的小时数、分钟数以及秒数获取实际课程耗时 */
    public static CourseCost fromAll(Integer allHours, Integer allMinutes, Integer allSeconds) {
        allMinutes += allSeconds / 60;
        allSeconds = allSeconds % 60;
        allHours += allMinutes / 60;
        allMinutes = allMinutes % 60;
        return new CourseCost().setHours(allHours).setMinutes(allMinutes).setSeconds(allSeconds);
    }

    /** 通过课程耗时列表获取实际课程耗时 */
    public static CourseCost fromList(List<CourseCost> courseCostList) {
        Integer hours = 0;
        Integer minutes = 0;
        Integer seconds = 0;
        for (CourseCost courseCost : courseCostList) {
            hours += courseCost.getHours();
            minutes += courseCost.getMinutes();
            seconds += courseCost.getSeconds();
        }
        return fromAll(hours, minutes, seconds);
    }

    /** 通过总秒数获取CourseCost */
    public static CourseCost secondToCourseCost(int seconds) {
        int hour = seconds / 3600;
        int other = seconds % 3600;
        int minute = other / 60;
        int second = other % 60;
        return new CourseCost().setHours(hour).setMinutes(minute).setSeconds(second);
    }

}
